package com.snj.test2508;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        String dateStr = "2025-04-01";
        List<List<String>> groupedDays = groupDaysInMonth(dateStr);
        groupedDays.forEach(System.out::println);
    }

    public static List<List<String>> groupDaysInMonth(String dateStr) {
        List<List<String>> groupedDays = new ArrayList<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // 解析传入的日期字符串
        LocalDate date = LocalDate.parse(dateStr, formatter);
        // 获取当前月份的天数
        int daysInMonth = date.lengthOfMonth();

        // 遍历当前月份的所有日期
        for (int day = 1; day <= daysInMonth; day += 3) {
            List<String> group = new ArrayList<>();
            for (int i = 0; i < 3; i++) {
                if (day + i <= daysInMonth) {
                    LocalDate currentDate = date.withDayOfMonth(day + i);
                    group.add(currentDate.format(formatter));
                } else {
                    // 不足的部分不补齐，直接结束内层循环
                    break;
                }
            }
            groupedDays.add(group);
        }

        // 后处理：将最后一条数据（小于 3 个日期）合并到倒数第二条数据中
        if (groupedDays.size() >= 2) {
            List<String> lastGroup = groupedDays.get(groupedDays.size() - 1);
            List<String> secondLastGroup = groupedDays.get(groupedDays.size() - 2);
            if (lastGroup.size() < 3) {
                secondLastGroup.addAll(lastGroup);
                groupedDays.remove(groupedDays.size() - 1);
            }
        }

        return groupedDays;
    }
}